using B.S.XZYData.Api.Read.Application.Command.SalesOrder;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 销售订单控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class SalesOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public SalesOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 查询销售订单列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="orderNo">订单号</param>
        /// <param name="remark">备注</param>
        /// <returns></returns>
        [HttpGet("QuerySalesOrder")]
        public async Task<ApiResult<ApiPageing<SalesOrderDTO>>> QuerySalesOrder(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10,
            [FromQuery] string? orderNo = null,
            [FromQuery] string? remark = null)
        {
            var command = new QuerySalesOrderCommand
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                OrderNo = orderNo,
                Remark = remark
            };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 查询销售订单产品明细
        /// </summary>
        /// <param name="salesOrderId">销售订单ID</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        [HttpGet("QuerySalesOrderProduct")]
        public async Task<ApiResult<ApiPageing<SalesOrderProductDTO>>> QuerySalesOrderProduct(
            [FromQuery] int salesOrderId,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var command = new QuerySalesOrderProductCommand
            {
                SalesOrderId = salesOrderId,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取产品选择列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="productCode">产品编码</param>
        /// <param name="productName">产品名称</param>
        /// <returns></returns>
        [HttpGet("GetProductSelect")]
        public async Task<ApiResult<ApiPageing<ProductSelectDTO>>> GetProductSelect(
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 30,
            [FromQuery] string? productCode = null,
            [FromQuery] string? productName = null)
        {
            var command = new GetProductSelectCommand
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                ProductCode = productCode,
                ProductName = productName
            };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 导出销售订单
        /// </summary>
        /// <param name="orderNo">单据编号（模糊查询）</param>
        /// <param name="remark">备注（模糊查询）</param>
        /// <param name="includeProductDetails">是否包含产品明细</param>
        /// <returns>Excel文件字节数组</returns>
        [HttpGet("ExportSalesOrder")]
        public async Task<IActionResult> ExportSalesOrder(
            [FromQuery] string? orderNo = null,
            [FromQuery] string? remark = null,
            [FromQuery] bool includeProductDetails = true)
        {
            var command = new ExportSalesOrderCommand
            {
                OrderNo = orderNo,
                Remark = remark,
                IncludeProductDetails = includeProductDetails
            };

            var result = await _mediator.Send(command);

            if (result.Code == ApiEnum.Success && result.Data != null)
            {
                var fileName = $"销售订单_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
                var encodedFileName = Uri.EscapeDataString(fileName);
                return File(result.Data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }

            return BadRequest(result.Msg);
        }
    }
}
